const ResHandler = require("../utils/ResHandler");
const JwtHandler = require('../utils/jwtHandler')
const resHandler = new ResHandler();
const jwtHandler = new JwtHandler();

const auth = async (req, res, next) => {
  try {
    const token = req.get('x-access-token')
    const result = await jwtHandler.verify(token)
    if(result.username) {
      next()
    } else {
      throw(new Error('Server Error'))
    }
  } catch (err) {
    res.json(resHandler.succRes({ code: 200, message: "请先登录！", data: false }));
  }
}

module.exports = {
  auth
}